package _12迭代器Iterator遍历集合;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class DemoIterator {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();


        //集合添加元素 add
        collection.add("java");
        collection.add("python");
        collection.add("c++");


        for (String s:collection){
            
            System.out.println(s);
        }


        Iterator<String>  itr = collection.iterator();

//        String next = itr.next();
//        System.out.println(next);
//        System.out.println(itr.next());
//        System.out.println(itr.next());
//        System.out.println(itr.next());//当集合中没有元素的时候，会报错java.util.NoSuchElementException


        while(itr.hasNext()){
            System.out.println(itr.next());
        }

    }
}
